<?php

namespace app\api\controller\questions;

use app\api\controller\Controllers;
use app\api\service\questions\Question as QuestionService;
use app\api\service\questions\DoAnswer as DoAnswerService;

class Question extends Controllers
{

    /**
     * 获取题库推荐
     * @param int $type
     * @param QuestionService $service
     * @return \think\Response
     */
    public function recommend(int $type, QuestionService $service)
    {
        return success($service->getType($type));
    }

    /**
     * 获取题目
     *
     * @param QuestionService $service
     * @param int $id
     * @return \think\Response
     */
    public function answer(QuestionService $service, int $id)
    {
        return success($service->detail($id));
    }

    /**
     * 根据考试类别获取题目
     *
     * @return \think\Response
     */
    public function category(QuestionService $service, int $category)
    {
        $get = $this->request->only(['examination_type'], 'get');
        $this->validate($get, 'app\api\validate\questions\Question.categoryControl');
        return success($service->category($category, $get));
    }

    /**
     * 获取做过的题目
     *
     * @param (DoAnswerService $service
     * @param int $id
     * @return \think\Response
     */
    public function doAnswer(DoAnswerService $service, int $id)
    {
        return success($service->doAnswer($id));
    }

    /**
     * 提交题目
     * @param DoAnswerService $service
     * @return \think\Response
     */
    public function submission(DoAnswerService $service)
    {
        $post = $this->request->only(['list','qid']);
        return $service->submission($post)?success():failed('提交失败,请重试');
    }

    /**
     * 查询我的错题列表
     * @param DoAnswerService $service
     * @return \think\Response
     */
    public function wrong(DoAnswerService $service)
    {
        $wrong = $service->wrong();
        return false===$wrong->isEmpty()?success($wrong):notFound();
    }

    /**
     * 查询错误的小题
     * @param DoAnswerService $service
     * @param int $id 大题ID
     * @return \think\Response
     */
    public function wrongAnswer(DoAnswerService $service, int $id)
    {
        return success($service->wrongAnswer($id));
    }

}
